package com.clean.cleantotal.compores.compress.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.RoundedCornersTransformation
import com.clean.cleantotal.R
import com.clean.cleantotal.base.BaseFragment
import com.clean.cleantotal.databinding.FragmentImageBinding
import com.clean.cleantotal.databinding.ItemCompressLayoutBinding
import com.clean.cleantotal.compores.compress.CompressDetailsActivity
import com.clean.cleantotal.utils.CompressUtils
import com.clean.cleantotal.utils.FormatterSize
import com.drake.brv.annotaion.DividerOrientation
import com.drake.brv.utils.divider
import com.drake.brv.utils.grid
import com.drake.brv.utils.setup
import com.luck.picture.lib.entity.LocalMedia


/**
 *  压缩
 */
class ImageFragment : BaseFragment<FragmentImageBinding>() {
    override fun getViewBinding(
        inflater: LayoutInflater, container: ViewGroup?
    ): FragmentImageBinding {
        return FragmentImageBinding.inflate(layoutInflater, container, false)
    }

    private var allPhotosList = mutableListOf<LocalMedia>()
    private var compressedList = mutableListOf<LocalMedia>()
    private var type = 0

    override fun initParams(arguments: Bundle?) {
        super.initParams(arguments)
        arguments?.let {
            type = it.getInt("type", 0)
        }
    }

    override fun initView() {
        if (type == 0) {
            CompressUtils.getAllPhotos(this, getString(R.string.app_name)) {
                allPhotosList.clear()
                allPhotosList.addAll(it)
                if (allPhotosList.isEmpty()) {
                    mBinding.stateLayout.showEmpty()
                } else {
                    mBinding.stateLayout.showContent()
                }
                mBinding.recyclerView.grid(3, orientation = RecyclerView.VERTICAL).divider {
                    setDivider(10, true)
                    orientation = DividerOrientation.VERTICAL
                }.setup {
                    addType<LocalMedia> { R.layout.item_compress_layout }
                    onBind {
                        val item = ItemCompressLayoutBinding.bind(itemView)
                        val data = getModel<LocalMedia>()
                        item.tvSize.text = FormatterSize.formatFileSize(context, data.size)
                        item.image.load(data.path) {
                            transformations(RoundedCornersTransformation(20f))
                        }
                    }
                    onClick(R.id.content) {
                        val model = getModel<LocalMedia>()
                        openActivity(CompressDetailsActivity::class.java, Bundle().apply {
                            putParcelable("media", model)
                            putString("state", "before")
                        })
                    }
                }.models = allPhotosList
            }
        } else {
            CompressUtils.getCompressedPhotos(this@ImageFragment) { folder ->
                val targetFolder = folder.find {
                    it.folderName.equals(
                        getString(R.string.app_name), ignoreCase = true
                    )
                }
                targetFolder?.let {
                    compressedList.clear()
                    compressedList.addAll(it.data)
                    if (compressedList.isEmpty()) {
                        mBinding.stateLayout.showEmpty()
                    } else {
                        mBinding.stateLayout.showContent()
                    }
                    mBinding.recyclerView.grid(3, orientation = RecyclerView.VERTICAL).divider {
                        setDivider(10, true)
                        orientation = DividerOrientation.VERTICAL
                    }.setup {
                        addType<LocalMedia>(R.layout.item_compress_layout)
                        onBind {
                            val item = ItemCompressLayoutBinding.bind(itemView)
                            val data = getModel<LocalMedia>()
                            item.tvSize.text = FormatterSize.formatFileSize(context, data.size)
                            item.image.load(data.path) {
                                transformations(RoundedCornersTransformation(20f))
                            }
                        }
                        onClick(R.id.content) {
                            val model = getModel<LocalMedia>()
                            openActivity(CompressDetailsActivity::class.java, Bundle().apply {
                                putParcelable("media", model)
                                putString("state", "after")
                            })
                        }
                    }.models = compressedList
                }
            }
        }
    }

    override fun initData(savedInstanceState: Bundle?) {
    }

}